# Enhanced regional analysis with better categories
regional_data <- Metropolitan_level_Zillow_Home_Value_Index %>%
mutate(
region = case_when(
StateName %in% c("CA", "OR", "WA") ~ "West Coast",
StateName %in% c("NY", "NJ", "CT", "MA", "RI", "NH", "VT") ~ "Northeast",
StateName %in% c("TX", "AZ", "CO", "NV", "UT", "ID") ~ "Mountain West",
StateName %in% c("FL", "GA", "NC", "SC", "TN", "AL") ~ "Southeast",
StateName %in% c("IL", "MI", "OH", "IN", "WI", "MN", "MO") ~ "Midwest",
TRUE ~ "Other Regions"
)
) %>%
group_by(region) %>%
summarize(
avg_growth = mean((`2024-01-31` - `2015-01-31`) / `2015-01-31` * 100, na.rm = TRUE),
avg_price_2015 = mean(`2015-01-31`, na.rm = TRUE),
avg_price_2024 = mean(`2024-01-31`, na.rm = TRUE),
market_count = n(),
.groups = 'drop'
) %>%
mutate(
price_increase = avg_price_2024 - avg_price_2015,
region = fct_reorder(region, avg_growth)
)
# Create interactive regional comparison with dual information
plot_ly(regional_data) %>%
add_trace(
x = ~region,
y = ~avg_growth,
type = 'bar',
name = 'Price Growth',
marker = list(
color = ~avg_growth,
colorscale = 'Viridis',
showscale = TRUE,
colorbar = list(title = list(text = "Growth %", side = "right"))
),
text = ~paste("<b>", round(avg_growth), "%</b>"),
textposition = 'outside',
hovertemplate = paste(
"<b>%{x}</b><br>",
"Average Growth (2015-2024): <b>%{y:.0f}%</b><br>",
"2024 Average Price: $%{customdata:,.0f}<br>",
"Markets in Region: %{text2}<extra></extra>"
),
customdata = ~avg_price_2024,
text2 = ~market_count
) %>%
add_trace(
x = ~region,
y = ~avg_price_2024,
type = 'scatter',
mode = 'markers+text',
marker = list(
symbol = 'diamond',
size = 12,
color = 'red',
line = list(color = 'white', width = 2)
),
name = '2024 Avg Price',
yaxis = 'y2',
text = ~paste("$", round(avg_price_2024/1000, 0), "K"),
textposition = 'middle right',
hovertemplate = paste(
"2024 Average Price: $%{y:,.0f}<extra></extra>"
)
) %>%
layout(
title = list(
text = "<b>Regional Housing Markets: Growth vs Prices (2015-2024)</b>",
x = 0.05,
font = list(size = 16)
),
xaxis = list(
title = "",
tickangle = -45
),
yaxis = list(
title = list(text = "Price Growth (%)", standoff = 20),
ticksuffix = "%",
gridcolor = '#e1e5ed'
),
yaxis2 = list(
title = list(text = "2024 Average Price", standoff = 20),
tickformat = "$,.0f",
overlaying = "y",
side = "right",
gridcolor = '#e1e5ed'
),
legend = list(
x = 0.02,
y = 0.98,
bgcolor = 'rgba(255,255,255,0.8)'
),
margin = list(r = 80),
plot_bgcolor = '#f8f9fa',
paper_bgcolor = '#f8f9fa'
)